package com.blb.bookms.mapper;

import com.blb.bookms.entity.Book;
import com.blb.bookms.entity.BookType;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 书籍的映射接口
 */
public interface BookMapper {

    /**
     * 根据类型id查询书籍
     * @param typeId
     * @return
     */
    @ResultMap("bookMap")
    @Select("select * from tb_book where type_id = #{typeId}")
    List<Book> selectBooksByTypeId(Long typeId);

    /**
     * 子查询实现关联查询
     * @return
     */
    @Results(id = "bookMap",
            value = {
                @Result(property = "id",column = "id",id = true),
                @Result(property = "name",column = "name"),
                @Result(property = "author",column = "author"),
                @Result(property = "price",column = "price"),
                @Result(property = "publisher",column = "publisher"),
                @Result(property = "picture",column = "picture"),
                @Result(property = "publishDate",column = "publish_date"),
                @Result(property = "typeId",column = "type_id"),
                @Result(property = "bookType",column = "type_id",javaType = BookType.class,
                        //子查询实现关联查询
//                        one = @One(select = "com.blb.bookms.mapper.BookTypeMapper.selectById")
                        //连接查询
                        one = @One(resultMap = "com.blb.bookms.mapper.BookTypeMapper.bookTypeMap")
                )
            })
    @Select("select b.id ,b.name,b.price,b.author,b.picture,b.publish_date,b.publisher,b.type_id,\n" +
            "t.id t_id,t.name t_name\n" +
            "from tb_book b join tb_book_type t\n" +
            "on b.type_id = t.id")
    List<Book> selectAll();

    @ResultMap("bookMap")
    @Select("select * from tb_book where id = #{id}")
    Book selectById(Long id);

    @Select("select * from tb_book where author = #{author}" +
            " and price between #{minPrice} and #{maxPrice}")
    List<Book> selectByPriceAndAuthor(@Param("author") String author,
                                      @Param("minPrice")double minPrice,
                                      @Param("maxPrice")double maxPrice);

    /**
     * 多条件查询
     * @param book
     * @return
     */
    @Select("<script>" +
            "        select * from tb_book\n" +
            "        <where>\n" +
            "            <if test=\"name != null\">\n" +
            "                and name = #{name}\n" +
            "            </if>\n" +
            "            <if test=\"author != null\">\n" +
            "                and author = #{author}\n" +
            "            </if>\n" +
            "            <if test=\"price != null\">\n" +
            "                and price = #{price}\n" +
            "            </if>\n" +
            "        </where>" +
            "</script>")
    List<Book> selectBooks(Book book);

    /**
     * 按id集合同时查询多本书
     * @param ids
     * @return
     */
    @Select("<script>        " +
            "       select * from tb_book\n" +
            "        where id in\n" +
            "        <foreach collection=\"ids\" item=\"id\" open=\"(\" close=\")\" separator=\",\">\n" +
            "            #{id}\n" +
            "        </foreach>" +
            "</script>")
    List<Book> selectBooksByIds(@Param("ids") List<Long> ids);

    @Insert("insert into tb_book(name,price,author,picture,publish_date,publisher,type_id)\n" +
            " values(#{name},#{price},#{author},#{picture},#{publishDate},#{publisher},#{typeId})")
    void insert(Book book);

    @Update("<script>" +
            "update tb_book\n" +
            "            <trim prefix=\"set\" suffixOverrides=\",\">\n" +
            "                <if test=\"name != null\">\n" +
            "                    name = #{name},\n" +
            "                </if>\n" +
            "                <if test=\"price != null\">\n" +
            "                    price = #{price},\n" +
            "                </if>\n" +
            "                <if test=\"author != null\">\n" +
            "                    author = #{author},\n" +
            "                </if>\n" +
            "                <if test=\"picture != null\">\n" +
            "                    picture = #{picture},\n" +
            "                </if>\n" +
            "                <if test=\"publishDate != null\">\n" +
            "                    publish_date = #{publishDate},\n" +
            "                </if>\n" +
            "                <if test=\"publisher != null\">\n" +
            "                    publisher = #{publisher},\n" +
            "                </if>\n" +
            "                <if test=\"typeId != null\">\n" +
            "                    type_id = #{typeId},\n" +
            "                </if>\n" +
            "            </trim>\n" +
            "            where id = #{id}" +
            "</script>")
    void update(Book book);

    @Delete("delete from tb_book where id = #{id}")
    void delete(Long id);
}
